home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 9445 < prev    next >
Encoding:
Text File  |  1996-08-05  |  8.0 KB  |  416 lines

  1. Path: lantana.singnet.com.sg!usenet
  2. From: Teddy Bear <s7700038@singnet.com.sg>
  3. Newsgroups: comp.lang.c
  4. Subject: Emergency C problem on fopen !
  5. Date: 10 Mar 1996 19:00:16 GMT
  6. Organization: Singapore Telecom Internet Service
  7. Message-ID: <4hv8s0$f81@lantana.singnet.com.sg>
  8. NNTP-Posting-Host: ts900-1131.singnet.com.sg
  9. Mime-Version: 1.0
  10. Content-Type: multipart/mixed;
  11.     boundary="-------------------------------30932167123963"
  12. X-Mailer: Mozilla 1.22 (Windows; I; 16bit)
  13.  
  14. This is a multi-part message in MIME format.
  15.  
  16. ---------------------------------30932167123963
  17. Content-Transfer-Encoding: 7bit
  18. Content-Type: text/plain; charset=us-ascii
  19.  
  20. I need help on this program
  21. Y can't i retrieve from disk or save to disk?
  22. Please help me.
  23.  
  24.  
  25. ---------------------------------30932167123963
  26. Content-Transfer-Encoding: 7bit
  27. Content-Type: text/plain
  28.  
  29. #include <bios.h>
  30. #include <stdio.h>
  31. #include <alloc.h>
  32. #include <conio.h>
  33. #include <string.h>
  34. #include <ctype.h>
  35. #include <stdlib.h>
  36. #define TRUE  1
  37. #define FALSE 0
  38.  
  39. typedef struct data{
  40.     int age;
  41.     char name[40];
  42.     char address[100];
  43.     struct data *next;
  44. }dataelm;
  45. typedef dataelm* dataptr;
  46.  
  47. void createdata(dataptr *ptrtohead, dataptr head);
  48. int  countlink(dataptr head);
  49. dataptr searchlink(dataptr head, char *wanted);
  50. void editlink(dataptr head);
  51. void display(dataptr head);
  52. void savefile(dataptr head);
  53. void readfile(dataptr head);
  54. dataptr clearlink(dataptr head);
  55. dataptr addnode(dataptr head);
  56. dataptr delnode(dataptr head);
  57. void check(dataptr head);
  58. int main()
  59. {
  60.     char *sname, option;
  61.     dataptr ptr, *ptrtohead, head, tail;
  62.  
  63.     head = NULL;
  64.     option = '1';
  65.     do
  66.     {
  67.     clrscr();
  68.     printf("\n\n\nThis program .....");
  69.     printf("\n\n1) Read existing list from disk\n");
  70.     printf("2) Add list\n");
  71.     printf("3) Delete list\n");
  72.     printf("4) Search list\n");
  73.     printf("5) Display list\n");
  74.     printf("6) Save list to disk\n");
  75.     printf("7) Kill Yi Yong !!!\n");
  76.     printf("\nPlease enter your choice : ");
  77.  
  78.     if(option < '1' || option > '7')
  79.         printf("\nInvaild option. Please re-enter:");
  80.     option = getche();
  81.     switch(option)
  82.     {
  83.         case '1' : readfile(head);
  84.                display(head);
  85.                printf("\n\nPress any key to continue.. ");
  86.                getch();
  87.                break;
  88.         case '2' : if(head == 0)
  89.                {
  90.                createdata(ptrtohead, NULL);
  91.                head = *ptrtohead;
  92.                }
  93.                else
  94.                head = addnode(head);
  95.                printf("\n\nPress any key to continue.. ");
  96.                getch();
  97.                break;
  98.         case '3' : head = delnode(head);
  99.                printf("\n\nPress any key to continue.. ");
  100.                getch();
  101.                break;
  102.         case '4' : editlink(head);
  103.                printf("\n\nPress any key to continue.. ");
  104.                getch();
  105.                break;
  106.         case '5' : display(head);
  107.                printf("\n\nPress any key to continue.. ");
  108.                getch();
  109.                break;
  110.         case '6' : savefile(head);
  111.                printf("\n\nPress any key to continue.. ");
  112.                getch();
  113.                break;
  114.         case '7' : clrscr();
  115.                exit(0);
  116.  
  117.     }
  118.     }
  119.     while(option != '7');
  120.     return 0;
  121. }
  122.  
  123.  
  124. void savefile(dataptr head)
  125.  
  126. {
  127.    dataelm *current;
  128.    FILE *f;
  129.    f=fopen("b:list.dat","wb");
  130.    current = head;
  131.    if (head!=NULL)
  132.     do
  133.     {
  134.     fwrite(current, sizeof(*current), 1, f);
  135.     current = current->next;
  136.     } while(current->next!=NULL);
  137.    fclose(f);
  138. }
  139.  
  140.  
  141.     void readfile(dataptr head)
  142. {
  143.       dataelm *new,*current;
  144.       FILE *f;
  145.       f=fopen("b:list.dat","rb");
  146.     while(!feof(f))
  147.     {
  148.     new=(dataelm *)malloc(sizeof(dataelm));
  149.     fread(new,sizeof(*new), 1, f);
  150.  
  151.      if (head==NULL)
  152.     {
  153.         head = new;
  154.         head->next = NULL;
  155.         current = head;
  156.     }
  157.      else
  158.     {
  159.         current->next = new;
  160.         new->next = NULL;
  161.         current = current->next;
  162.     }
  163.     }
  164.  
  165.         fclose(f);
  166. }
  167.  
  168. void createdata(dataptr *ptrtohead, dataptr head)
  169.  
  170. {
  171.     dataptr temp, last;
  172.     char answer;
  173.     int elm_size = sizeof(dataelm);
  174.     do
  175.     {
  176.     temp=(dataptr)malloc(elm_size);
  177.     clrscr();
  178.     printf("\nEnter Name    : ");
  179.     gets(temp->name);
  180.     printf("Enter Age     : ");
  181.     scanf("%d",&temp->age);
  182.     fflush(stdin);
  183.     printf("Enter Address : ");
  184.     gets(temp->address);
  185.  
  186.     if(head == NULL)
  187.     {
  188.         head = temp;
  189.         last = temp;
  190.     }
  191.     else
  192.     {
  193.         last->next=temp;
  194.         last = temp;
  195.     }
  196.     printf("\nWould u like to enter more ? <Y/N> : ");
  197.     answer=toupper(getch());
  198.     }
  199.     while(answer != 'N');
  200.     last->next = NULL;
  201.     *ptrtohead = head;
  202. }
  203.  
  204. dataptr searchlink(dataptr head, char *wanted)
  205. // search the link for an element.
  206. {
  207.     int element, count, found, condition;
  208.     dataptr ptr;
  209.     element = countlink(head);
  210.     ptr = head;
  211.     found = FALSE;
  212.     condition = TRUE;
  213.     for(count=0; (count<element)&&(condition); )
  214.     {
  215.     if((strcmp(ptr->name, wanted))==0)
  216.     {
  217.         found = TRUE;
  218.         condition = FALSE;
  219.     }
  220.     else
  221.         if((strcmp(ptr->name, wanted)) > 0)
  222.         condition = FALSE;
  223.         else
  224.         {
  225.         ptr = ptr->next;
  226.         count++;
  227.         }
  228.     }
  229.  
  230.     if(found)
  231.     return ptr;
  232.     else
  233.     return NULL;
  234. }
  235.  
  236. int countlink(dataptr head)
  237. // count the number of elements in the link.
  238. {
  239.     int count;
  240.     dataptr ptr;
  241.     ptr = head;
  242.     if(ptr != NULL)
  243.     {
  244.     for(count=1;ptr->next!=NULL; count++)
  245.     ptr = ptr->next;
  246.     return count;
  247.     }
  248.     else
  249.     {
  250.     return 0;
  251.     }
  252. }
  253.  
  254. void  editlink(dataptr head)
  255. // Edit a specifly element in the link pointed by the
  256. // pointer "ptrtoelm"
  257. {
  258.  
  259.     char *find, *temptol,*temptel, *temptal, answer='Y';
  260.     int cnt, check;
  261.     dataptr ptrtoelm;
  262.     do
  263.     {
  264.     clrscr();
  265.     printf("\n\nEnter Name   :  ");
  266.     gets(find);
  267.     ptrtoelm = searchlink(head, find);
  268.     if(ptrtoelm==NULL)
  269.     {
  270.         printf("\a\nSORRY! THERE IS NO SUCH NAME\n");
  271.         printf("\nDo you wish to continue searching? <Y/N> : ");
  272.         answer = toupper(getch());
  273.     }
  274.     if(answer == 'N')
  275.         return;
  276.     }while(ptrtoelm==NULL && answer == 'Y');
  277.  
  278.  
  279.     printf("\nName    : %s\n", ptrtoelm->name);
  280.     printf("Age     : %d\n", ptrtoelm->age);
  281.     printf("Address : %s\n", ptrtoelm->address);
  282. }
  283.  
  284.  
  285. void display(dataptr head)
  286. // Display the information in the link.
  287. {
  288.     dataptr ptr;
  289.     ptr = head;
  290.     clrscr();
  291.     while(ptr!=NULL)
  292.     {
  293.     printf("\nNAME: %s \n",ptr->name);
  294.     printf("AGE: %d \n",ptr->age);
  295.     printf("ADDRESS: %s \n",ptr->address);
  296.     ptr = ptr->next;
  297.     }
  298.  
  299. }
  300.  
  301. dataptr clearlink(dataptr head)
  302. // clear all data and free all memory used by the data.
  303. {
  304.     dataptr ptr;
  305.     while(head != NULL)
  306.     {
  307.     ptr = head;
  308.     head = head->next;
  309.     free(ptr);
  310.     }
  311.     free(ptr);
  312.     return head;
  313. }
  314.  
  315. dataptr addnode(dataptr head)
  316. // Add a node to the link
  317. {
  318.  
  319.     dataptr front, back, newitem;
  320.     int check, cnt;
  321.     newitem = (dataptr)malloc(sizeof(dataelm));
  322.     clrscr();
  323.     printf("\n\nEnter name to be added : ");
  324.     gets(newitem->name);
  325.     if((strcmp((back->name),(front->name))) == 0)
  326.     {
  327.     printf("\a");
  328.     printf("\n\nDUPLICATE COPY");
  329.     strcpy(back->name,front->name);
  330.     back->age = front->age;
  331.     strcpy(back->address,front->address);
  332.     getch();
  333.     }
  334.     fflush(stdin);
  335.     printf("Enter age              : ");
  336.     scanf("%d", &newitem->age);
  337.     fflush(stdin);
  338.     printf("Enter address          : ");
  339.     gets(newitem->address);
  340.     if((strcmp(newitem->name, head->name)) <= 0)
  341.     {
  342.     newitem->next = head;
  343.     head = newitem;
  344.     }
  345.     else
  346.     {
  347.     back = head;
  348.     front = back->next;
  349.     while(front!=NULL)
  350.     {
  351.         if((strcmp(newitem->name, front->name)) > 0)
  352.         {
  353.         back = front;
  354.         front = front->next;
  355.         }
  356.         else
  357.         break;
  358.     }
  359.     newitem->next = front;
  360.     back->next = newitem;
  361.     }
  362.     return head;
  363. }
  364.  
  365. dataptr delnode(dataptr head)
  366. // Delete a node from a link.
  367. {
  368.     dataptr front, back;
  369.     char *tempname;
  370.     if(head != NULL)
  371.     {
  372.     back = head;
  373.     front = back->next;
  374.     }
  375.     else
  376.     {
  377.     printf("\n\aDelete not allowed.");
  378.     getch();
  379.     return NULL;
  380.     }
  381.     tempname = (char *)malloc(sizeof(char *));
  382.     clrscr();
  383.     printf("\n\nEnter name to be delete : ");
  384.     gets(tempname);
  385.     if((strcmp(tempname,back->name)) == 0)
  386.     {
  387.     front = head;
  388.     back = back->next;
  389.     free(front);
  390.     return back;
  391.     }
  392.     else
  393.     {
  394.     while(((strcmp(tempname, front->name))!=0) && (front!=NULL))
  395.     {
  396.         back = front;
  397.         front = front->next;
  398.     }
  399.     if(front==NULL)
  400.     {
  401.         printf("\n\aNo such person in the list.");
  402.         return head;
  403.     }
  404.     else
  405.     {
  406.         back->next = front->next;
  407.         free(front);
  408.         return head;
  409.     }
  410.     }
  411.  
  412. }
  413.  
  414.  
  415. ---------------------------------30932167123963--
  416.